package net.starwifi.duobao.web;

import net.starwifi.duobao.entity.SmsTemplate;
import net.starwifi.duobao.mvc.Message;
import net.starwifi.duobao.mvc.PageView;
import net.starwifi.duobao.service.SmsTemplateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by Administrator on 2015/7/20.
 */
@Controller("adminSmsTemplateController")
@RequestMapping("/smsTemplate")
public class SmsTemplateController extends BaseController {


    @Autowired
    SmsTemplateService smsTemplateService;

    /**
     * 添加
     */
    @RequestMapping(value = "/add", method = RequestMethod.GET)
    public String add(ModelMap model) {
        return "/smsTemplate/add";
    }

    /**
     * 保存
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public String save(SmsTemplate smsTemplate,  RedirectAttributes redirectAttributes) {
        if (!isValid(smsTemplate)) {
            return ERROR_VIEW;
        }
        smsTemplateService.save(smsTemplate);
        addFlashMessage(redirectAttributes, SUCCESS_MESSAGE);
        return "redirect:list.do";
    }



    /**
     * 编辑
     */
    @RequestMapping(value = "/edit", method = RequestMethod.GET)
    public String edit(Long id, ModelMap model) {
        model.addAttribute("entity", smsTemplateService.find(id));
        return "/smsTemplate/edit";
    }

    /**
     * 更新
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public String update(SmsTemplate smsTemplate, RedirectAttributes redirectAttributes) {
        if (!isValid(smsTemplate)) {
            return ERROR_VIEW;
        }
        smsTemplateService.updateEntity(smsTemplate,smsTemplate.getId());
        addFlashMessage(redirectAttributes, SUCCESS_MESSAGE);
        return "redirect:list.do";
    }



    /**
     * 列表
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public String list(PageView pageView, ModelMap model,HttpServletRequest request) {
        pageView.filterQuery(request);
        model.addAttribute("page", smsTemplateService.getScrollData(pageView));
        return "/smsTemplate/list";
    }

    /**
     * 删除
     */
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public @ResponseBody
    Message delete(Long[] ids) { smsTemplateService.delete(ids);
        return SUCCESS_MESSAGE;
    }

}
